Making Rollator print pretty, or: mod_rewrite and you, keep it clean!

It\’s been around for years, and I\’ve used it countless times.

Now that everything\’s finally stablized, I\’m happy to go back and put the \’finishing tweaks\’ together to make my site have a clean, smooth flow.

By this, I mean, rewriting various requests to proper bits. The easiest way to do this is with your apache conf; .htaccess in whatever directory you\’re working in. As I have Rollator managing my entire site, this is my document root.

Since I know I have mod_rewrite, I\’ve created a new variable in my Rollator configuration file (rollator.inc):

$rewrite=\’1\’; // Apache mod_rewrite

For this example, I\’ve also slightly modified rollator\’s internals. Please note that I\’ll try to integrate this in the next release; but for the sake of keeping this document brief, let us assume I modified the search() function as such:

if ($rewrite) { print \”[$qbtype] $qbtitle\”;
} else { print \”[$qbtype] $qbtitle\”;
}

This presupposes that someone gets a \’hit\’ for a search function, and the above prints out the url. Since I\’ve set $rewrite, it\’s going to use the first function, which is \”/view/$qbid\”, or the numerical representation of that entry.

The second is the actual means of Rollator\’s reading and storage facility, which we want to hide with Apache\’s mod_rewrite.

Since we\’re positive we have support for mod_rewrite, we can modify our local .htaccess as follows:

RewriteEngine On RewriteRule ^view$ /index.php RewriteRule ^view/$ /index.php RewriteRule ^view/(.)$ /index.php?op=view&qbid=$1

The first statement says, \”If mod_rewrite is loaded, then continue.\” This is always best, lest your web site become entirely inaccessable if Apache gets screwed up.

The second line turns on the Rewrite engine.

The third is a niceity, just in case your machine isn\’t setup properly, or your hosting might be a bit strict. It says \’if someone goes to /view by itself, then redirect to /index.php\’, which is where in this example we have Rollator installed, which defaults to printing the basic news – more than likely what they were looking for anyhow.

The fourth line is similar to the third; assuming they access by directory with no numeric.

The fifth line is the meat. This says \’if someone goes to /view/, snarf whatever we have AFTER that, and redirect to /index.php (rollator), set request for op=view (display), &qbid= whatever they sent. We\’re assuming here that they\’re giving an appropriate number, or following a rollator created link. So, if someone goes to /view/32, mod_rewrite rewrites that to /index.php?op=view&qbid=32.

Now, assume someone wanted to check our archives – Rollator\’s archives function is op=arc&dot=timestamp, so, we could do something such as:

RewriteRule ^archive/(.)$ /index.php?op=arc&dot=$1

Which follows the same conventions as our above statements, and looks clean – hiding some of our script internals, to boot!

Fairly simple, huh? There are much better ways to do the above, condensing, and testing variables, but for the scope of this little mini-howto, it should get you on the road to writing your own rewrite rules.

A good document to check out is the Apache 1.3 URL Rewriting Guide.